package com.facebook.drawee.generic;

import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;

import com.facebook.common.internal.Preconditions;
import com.facebook.drawee.drawable.DrawableParent;
import com.facebook.drawee.drawable.ForwardingDrawable;
import com.facebook.drawee.drawable.Rounded;
import com.facebook.drawee.drawable.RoundedBitmapDrawable;
import com.facebook.drawee.drawable.RoundedColorDrawable;
import com.facebook.drawee.drawable.RoundedCornersDrawable;
import com.facebook.drawee.drawable.ScaleTypeDrawable;
import com.facebook.drawee.drawable.ScalingUtils;

import javax.annotation.Nullable;

public final class WrappingUtils {
    private static final Drawable a = new ColorDrawable(0);

    @Nullable
    static Drawable maybeWrapWithScaleType(@Nullable Drawable drawable) {
        return drawable;
    }

    static Drawable maybeWrapWithRoundedOverlayColor(@Nullable Drawable arg2, @Nullable RoundingParams arg3) {
        if (arg2 != null && arg3 != null && arg3.mRoundingMethod == RoundingParams.RoundingMethod.OVERLAY_COLOR) {
            RoundedCornersDrawable v0 = new RoundedCornersDrawable(arg2);
            WrappingUtils.a(((Rounded) v0), arg3);
            v0.setOverlayColor(arg3.mOverlayColor);
            arg2 = v0;
        }

        return arg2;
    }

    @Nullable
    static Drawable maybeWrapWithScaleType(@Nullable Drawable arg1, @Nullable ScalingUtils.ScaleType arg2) {
        return WrappingUtils.b(arg1, arg2);
    }

    static Drawable maybeApplyLeafRounding(@Nullable Drawable arg2, @Nullable RoundingParams arg3, Resources arg4) {
        if (arg2 != null && arg3 != null && arg3.mRoundingMethod == RoundingParams.RoundingMethod.BITMAP_ONLY) {
            if ((arg2 instanceof ForwardingDrawable)) {
                DrawableParent v0 = a((DrawableParent) arg2);
                v0.setCurrent(WrappingUtils.b(v0.setCurrent(WrappingUtils.a), arg3, arg4));
            } else {
                arg2 = WrappingUtils.b(arg2, arg3, arg4);
            }
        }

        return arg2;
    }

    static void updateOverlayColorRounding(DrawableParent arg3, @Nullable RoundingParams arg4) {
        Drawable v0 = arg3.getDrawable();
        if (arg4 == null || arg4.mRoundingMethod != RoundingParams.RoundingMethod.OVERLAY_COLOR) {
            if ((v0 instanceof RoundedCornersDrawable)) {
                arg3.setCurrent(((RoundedCornersDrawable) v0).setCurrentWithoutInvalidate(WrappingUtils.a));
                WrappingUtils.a.setCallback(null);
            }
        } else if ((v0 instanceof RoundedCornersDrawable)) {
            WrappingUtils.a(((Rounded) v0), arg4);
            ((RoundedCornersDrawable) v0).setOverlayColor(arg4.mOverlayColor);
        } else {
            arg3.setCurrent(WrappingUtils.maybeWrapWithRoundedOverlayColor(arg3.setCurrent(WrappingUtils.a), arg4));
        }
    }

    static void updateLeafRounding(DrawableParent arg6, @Nullable RoundingParams arg7, Resources arg8) {
        DrawableParent v1 = WrappingUtils.a(arg6);
        Drawable v0 = v1.getDrawable();
        if (arg7 == null || arg7.mRoundingMethod != RoundingParams.RoundingMethod.BITMAP_ONLY) {
            if ((v0 instanceof Rounded)) {
                ((Rounded) v0).setCircle(false);
                ((Rounded) v0).setRadius();
                ((Rounded) v0).setBorder(0, 0f);
                ((Rounded) v0).setPadding(0f);
            }
        } else if ((v0 instanceof Rounded)) {
            WrappingUtils.a(((Rounded) v0), arg7);
        } else if (v0 != null) {
            v1.setCurrent(WrappingUtils.a);
            v1.setCurrent(WrappingUtils.b(v0, arg7, arg8));
        }
    }

    private static void a(Rounded arg2, RoundingParams arg3) {
        arg2.setCircle(arg3.mRoundAsCircle);
        arg2.setRadii(arg3.mCornersRadii);
        arg2.setBorder(arg3.mBorderColor, arg3.mBorderWidth);
        arg2.setPadding(arg3.mPadding);
    }

    private static DrawableParent a(DrawableParent arg2) {
        while (true) {
            Drawable v0 = arg2.getDrawable();
            if (v0 != arg2 && ((v0 instanceof DrawableParent))) {
                arg2 = (DrawableParent) v0;
                continue;
            }
            break;
        }

        return ((DrawableParent) arg2);
    }

    public static ScaleTypeDrawable a(DrawableParent arg2, ScalingUtils.ScaleType arg3) {
        Drawable v0 = WrappingUtils.b(arg2.setCurrent(WrappingUtils.a), arg3);
        arg2.setCurrent(v0);
        Preconditions.checkNotNull(v0, "Parent has no child drawable!");
        return ((ScaleTypeDrawable) v0);
    }

    @Nullable
    static Drawable b(@Nullable Drawable drawable, @Nullable ScalingUtils.ScaleType scaleType) {
        if (drawable != null && scaleType != null) {
            drawable = new ScaleTypeDrawable(drawable, scaleType);
        }
        return drawable;
    }

    private static Drawable b(Drawable arg2, RoundingParams arg3, Resources arg4) {
        RoundedColorDrawable v2_1;
        if ((arg2 instanceof BitmapDrawable)) {
            RoundedBitmapDrawable v2 = RoundedBitmapDrawable.fromBitmapDrawable(arg4, ((BitmapDrawable) arg2));
            WrappingUtils.a(((Rounded) v2), arg3);
            return v2;
        } else if (((arg2 instanceof ColorDrawable)) && Build.VERSION.SDK_INT >= 11) {
            v2_1 = RoundedColorDrawable.fromColorDrawable(((ColorDrawable) arg2));
            WrappingUtils.a(((Rounded) v2_1), arg3);
            return v2_1;
        }

        return arg2;
    }
}

